//=======================================================================
//功能: 			JavaScript main
//author:		岳静
//e-mail: 		yuejing0129@126.com
//QQ:			503490146
//date: 		2012-08-30
//version: 		1.0
//=======================================================================
$(function() {
	header.selectOperateMenu('main');

	//处理传值过来的事件
	var _paramType = $('#paramType');
	if(_paramType.val() === '') {
		//选中第一项
		$(".left_menu")[0].click();
	}
	else if(_paramType.val() === 'mMyAttention') {
		pbClickMain(document.getElementById('mMyAttention'), 'myAttention(1)');
	} else if(_paramType.val() === 'mMyFans') {
		pbClickMain(document.getElementById('mMyFans'), 'myFans(1)');
	}
});

//保存关注/取消粉丝的类型数据
var jsonType = {};

/**
 * 菜单点击事件
 * @param _this		: 当前选中的对象
 * @param _callback	: 回调函数
 */
function pbClickMain(_this, _callback) {
	var _obj = undefined;
	var _menu = $(".left_menu");
	var _thisId = $(_this).attr('id');
	$.each(_menu, function(i, obj) {
		var _obj = $(obj);
		if(_obj.attr('id') === _thisId) {
			_obj.attr("class", "left_menu left_hover");
		} else {
			_obj.attr("class", "left_menu");
		}
	});
	eval(_callback);

	var _isAfShow = false;
	var _isCaShow = false;
	switch (_thisId) {
	case "mOnewayAttention":
		//单向关注
		jsonType.fansAttentionType = "onewayAttention";
		jsonType.type = "attention";
		jsonType.opterType = "onewayAttention";
		_isAfShow = false;
		_isCaShow = true;
		break;
	case "mOnewayFans":
		//单向粉丝
		jsonType.fansAttentionType = "onewayFans";
		jsonType.type = "fans";
		jsonType.opterType = "onewayFans";
		_isAfShow = true;
		_isCaShow = false;
		break;
	case "mMutualPowderFans":
		//互粉粉丝
		jsonType.fansAttentionType = "mutualPowderFans";
		jsonType.type = "fans";
		jsonType.opterType = "mutualPowderFans";
		_isAfShow = false;
		_isCaShow = true;
		break;
	case "mMyAttention":
		//我的关注
		jsonType.fansAttentionType = "myAttention";
		jsonType.type = "attention";
		jsonType.opterType = "myAttention";
		_isAfShow = false;
		_isCaShow = true;
		break;
	case "mMyFans":
		//我的粉丝
		jsonType.fansAttentionType = "myFans";
		jsonType.type = "fans";
		jsonType.opterType = "myFans";
		_isAfShow = true;
		_isCaShow = true;
		break;
	}
	if(_isAfShow)
		$('#attentionFans').fadeIn(1000);
	else
		$('#attentionFans').fadeOut(1000);
	if(_isCaShow)
		$('#cancelAttention').fadeIn(1000);
	else
		$('#cancelAttention').fadeOut(1000);
}

/**
 * 关注粉丝
 * @param _fansType : 刷新数据的类型
 * @param _type		: 类型
 * @param _opterType: 操作类型
 */
function attentionFans() {
	var _fansType = jsonType.fansAttentionType;
	var _type = jsonType.type;
	var _opterType = jsonType.opterType;
	var weiboChk = $('input:checkbox[class=weiboChk]:checked');
	if(weiboChk.size() === 0) {
		alert("请您至少选择一个用户!");
		return;
	}

	var uids = "";
	$.each(weiboChk, function(i, obj) {
		uids += $(obj).val() + ",";
	});
	jQuery.ajax({
		url : webroot + "/fans/json/attentionFans.shtml",
			data : {
			"uids" : uids,
			"type" : _type,
			"opterType" : _opterType
		},
		type : "POST",
		dataType : "json",
		async : true,
		beforeSend: function(){
			//提交处理的函数
			loading("关注中...");
		},
		error : function(json){
			alert("关注出错了~");
			unloading();
		},
		//返回的是一个对象
		success : function(json){
			_msg = json.weiboMsg;
			//重新加载信息
			switch (_fansType) {
			case "onewayFans":
				//单向粉丝
				onewayFans(1);
				break;
			case "myFans":
				//我的粉丝
				myFans(1);
				break;
			}

			if(json.result === "error_weibo") {
				_msg += ' { ' + json.errorWeiboMsg + ' } ';
			}
			else if(json.result === "error") {
				_msg += ' { 请求操作, 系统出现异常! } ';
			}
			alert(_msg);
			unloading();

			$('#fansInfoSelectAll').attr('checked', false);
		}
	});
}

/**
 * 取消关注
 * @param _attentionType : 刷新数据的类型
 * @param _type			 : 类型
 * @param _opterType: 操作类型
 */
function cancelAttention() {
	var _attentionType = jsonType.fansAttentionType;
	var _type = jsonType.type;
	var _opterType = jsonType.opterType;
	var weiboChk = $('input:checkbox[class=weiboChk]:checked');
	if(weiboChk.size() === 0) {
		alert("请您至少选择一个用户!");
		return;
	}

	var uids = "";
	$.each(weiboChk, function(i, obj) {
		uids += $(obj).val() + ",";
	});
	jQuery.ajax({
		url : webroot + "/attention/json/cancelAttention.shtml",
			data : {
			"uids" : uids,
			"type" : _type,
			"opterType" : _opterType
		},
		type : "POST",
		dataType : "json",
		async : true,
		beforeSend: function(){
			//提交处理的函数
			loading("取消单向关注中...");
		},
		error : function(json){
			alert("取消单向关注出错了~");
			unloading();
		},
		//返回的是一个对象
		success : function(json){
			var _msg = json.weiboMsg;
			//重新加载信息
			switch (_attentionType) {
			case "onewayAttention":
				//单向关注
				onewayAttention(1);
				break;
			case "myAttention":
				//我的关注
				myAttention(1);
				break;
			case "mutualPowderFans":
				//互粉粉丝
				mutualPowderFans(1);
				break;
			}

			if(json.result === "error_weibo") {
				_msg += ' { ' + json.errorWeiboMsg + ' } ';
			}
			else if(json.result === "error") {
				_msg += ' { 请求操作, 系统出现异常! } ';
			}
			alert(_msg);
			unloading();

			$('#fansInfoSelectAll').attr('checked', false);
		}
	});
}

/**
 * 全选
 * @param _thisName :	点击的名称
 * @param _name		:	class的名称[目前没用到]
 */
function selectAll(_thisName, _name) {
	var weiboChk = $(".weiboChk");
	var _bool = $("#" + _thisName).attr("checked");
	if(_bool === undefined) {
		_bool = false;
	}
	$.each(weiboChk, function(i, obj) {
		$(obj).attr("checked", _bool);
	});
}

//获取单向关注
var onewayAttentionPage = new Page("onewayAttentionPage", onewayAttention, "bodyInfo", "bodyInfoPage");
onewayAttentionPage.size = 50;
onewayAttentionPage.pageSizeArr = [50, 70, 90];
//处理Page标签的函数
function onewayAttention(_page) {
	if(_page != undefined) {
		onewayAttentionPage.page = _page;
	}
	jQuery.ajax({
		url : webroot + "/attention/json/onewayAttention.shtml",
		data : {
		"page" : onewayAttentionPage.page,
		"size" : onewayAttentionPage.size
	},
	type : "POST",
	dataType : "json",
	async : true,
	beforeSend: function(){
		//提交处理的函数
		onewayAttentionPage.pageBeforeSend("<li>加载单向关注中...</li>");
		loading("加载单向关注中...");
	},
	error : function(json){
		onewayAttentionPage.pageError("<li>加载单向关注出错了~</li>");
		unloading();
	},
	//返回的是一个对象
	success : function(json){
		if(json.result === "success") {
			//处理数据列表的行信息
			function getResult(obj) {
				var result = '<li><label><div class="floatleft"><input type="checkbox" class="weiboChk" value="' + obj.fansattentionusername + '"/><img width="50" height="50" src="' + obj.img + '"/></div><div class="floatright"><p title="' + obj.nickname + '"><a href="http://weibo.com/u/' + obj.fansattentionusername + '" target="_banlk">' + obj.nickname + '</a></p>';
				result += '<p><img src="' + webroot + '/resources/images/' + (obj.gender === '男' ? 'user_man.png' : 'user_woman.png') + '" alt="' + obj.gender + '" title="' + obj.gender + '"/>&nbsp;';
				if(obj.verified === 'true') {
					result += '<img src="' + webroot + '/resources/images/weibo/weibo_v_org.png" alt="已V认证" title="已V认证"/>';
				}
				result += '</p></div></label></li>';
				return result;
			}
			onewayAttentionPage.pageOperate(json.data, { resultFn : getResult, dataNull : "<li>没有查找到单向关注数据!</li>", pageFootStyle : onewayAttentionPage.pageStyle.defaultStyle });
		}
		else if(json.result === "error_weibo") {
			alert(json.weiboMsg);
		}
		else if(json.result === "error") {
			alert("请求操作, 系统出现异常!");
		}
		unloading();
	}
	});
}

//获取单向粉丝
var onewayFansPage = new Page("onewayFansPage", onewayFans, "bodyInfo", "bodyInfoPage");
onewayFansPage.size = 50;
onewayFansPage.pageSizeArr = [50, 70, 90];
//处理Page标签的函数
function onewayFans(_page) {
	if(_page != undefined) {
		onewayFansPage.page = _page;
	}
	jQuery.ajax({
		url : webroot + "/fans/json/onewayFans.shtml",
		data : {
		"page" : onewayFansPage.page,
		"size" : onewayFansPage.size
	},
	type : "POST",
	dataType : "json",
	async : true,
	beforeSend: function(){
		//提交处理的函数
		onewayFansPage.pageBeforeSend("<li>加载单向粉丝中...</li>");
		loading("加载单向粉丝中...");
	},
	error : function(json){
		onewayFansPage.pageError("<li>加载单向粉丝出错了~</li>");
		unloading();
	},
	//返回的是一个对象
	success : function(json){
		if(json.result === "success") {
			//处理数据列表的行信息
			function getResult(obj) {
				var result = '<li><label><div class="floatleft"><input type="checkbox" class="weiboChk" value="' + obj.fansattentionusername + '"/><img width="50" height="50" src="' + obj.img + '"/></div><div class="floatright"><p title="' + obj.nickname + '"><a href="http://weibo.com/u/' + obj.fansattentionusername + '" target="_banlk">' + obj.nickname + '</a></p>';
				result += '<p><img src="' + webroot + '/resources/images/' + (obj.gender === '男' ? 'user_man.png' : 'user_woman.png') + '" alt="' + obj.gender + '" title="' + obj.gender + '"/>&nbsp;';
				if(obj.verified === 'true') {
					result += '<img src="' + webroot + '/resources/images/weibo/weibo_v_org.png" alt="已V认证" title="已V认证"/>';
				}
				result += '</p></div></label></li>';
				return result;
			}
			onewayFansPage.pageOperate(json.data, { resultFn : getResult, dataNull : "<li>没有查找到单向粉丝数据!</li>", pageFootStyle : onewayFansPage.pageStyle.defaultStyle });
		}
		else if(json.result === "error_weibo") {
			alert(json.weiboMsg);
		}
		else if(json.result === "error") {
			alert("请求操作, 系统出现异常!");
		}
		unloading();
	}
	});
}

//获取互粉粉丝
var mutualPowderFansPage = new Page("mutualPowderFansPage", mutualPowderFans, "bodyInfo", "bodyInfoPage");
mutualPowderFansPage.size = 50;
mutualPowderFansPage.pageSizeArr = [50, 70, 90];
//处理Page标签的函数
function mutualPowderFans(_page) {
	if(_page != undefined) {
		mutualPowderFansPage.page = _page;
	}
	jQuery.ajax({
		url : webroot + "/fans/json/mutualPowderFans.shtml",
		data : {
		"page" : mutualPowderFansPage.page,
		"size" : mutualPowderFansPage.size
	},
	type : "POST",
	dataType : "json",
	async : true,
	beforeSend: function(){
		//提交处理的函数
		mutualPowderFansPage.pageBeforeSend("<li>加载互粉粉丝中...</li>");
		loading("加载互粉粉丝中...");
	},
	error : function(json){
		mutualPowderFansPage.pageError("<li>加载互粉粉丝出错了~</li>");
		unloading();
	},
	//返回的是一个对象
	success : function(json){
		if(json.result === "success") {
			//处理数据列表的行信息
			function getResult(obj) {
				var result = '<li><label><div class="floatleft"><input type="checkbox" class="weiboChk" value="' + obj.fansattentionusername + '"/><img width="50" height="50" src="' + obj.img + '"/></div><div class="floatright"><p title="' + obj.nickname + '"><a href="http://weibo.com/u/' + obj.fansattentionusername + '" target="_banlk">' + obj.nickname + '</a></p>';
				result += '<p><img src="' + webroot + '/resources/images/' + (obj.gender === '男' ? 'user_man.png' : 'user_woman.png') + '" alt="' + obj.gender + '" title="' + obj.gender + '"/>&nbsp;';
				if(obj.verified === 'true') {
					result += '<img src="' + webroot + '/resources/images/weibo/weibo_v_org.png" alt="已V认证" title="已V认证"/>';
				}
				result += '</p></div></label></li>';
				return result;
			}
			mutualPowderFansPage.pageOperate(json.data, { resultFn : getResult, dataNull : "<li>没有查找到互粉粉丝数据!</li>", pageFootStyle : mutualPowderFansPage.pageStyle.defaultStyle });
		}
		else if(json.result === "error_weibo") {
			alert(json.weiboMsg);
		}
		else if(json.result === "error") {
			alert("请求操作, 系统出现异常!");
		}
		unloading();
	}
	});
}

//获取我的关注
var myAttentionPage = new Page("myAttentionPage", myAttention, "bodyInfo", "bodyInfoPage");
myAttentionPage.size = 50;
myAttentionPage.pageSizeArr = [50, 70, 90];
//处理Page标签的函数
function myAttention(_page) {
	if(_page != undefined) {
		myAttentionPage.page = _page;
	}
	jQuery.ajax({
		url : webroot + "/attention/json/myAttention.shtml",
		data : {
		"page" : myAttentionPage.page,
		"size" : myAttentionPage.size
	},
	type : "POST",
	dataType : "json",
	async : true,
	beforeSend: function(){
		//提交处理的函数
		myAttentionPage.pageBeforeSend("<li>加载我的关注中...</li>");
		loading("加载我的关注中...");
	},
	error : function(json){
		myAttentionPage.pageError("<li>加载我的关注出错了~</li>");
		unloading();
	},
	//返回的是一个对象
	success : function(json){
		if(json.result === "success") {
			//处理数据列表的行信息
			function getResult(obj) {
				var result = '<li><label><div class="floatleft"><input type="checkbox" class="weiboChk" value="' + obj.fansattentionusername + '"/><img width="50" height="50" src="' + obj.img + '"/></div><div class="floatright"><p title="' + obj.nickname + '"><a href="http://weibo.com/u/' + obj.fansattentionusername + '" target="_banlk">' + obj.nickname + '</a></p>';
				result += '<p><img src="' + webroot + '/resources/images/' + (obj.gender === '男' ? 'user_man.png' : 'user_woman.png') + '" alt="' + obj.gender + '" title="' + obj.gender + '"/>&nbsp;';
				if(obj.verified === 'true') {
					result += '<img src="' + webroot + '/resources/images/weibo/weibo_v_org.png" alt="已V认证" title="已V认证"/>';
				}
				result += '</p></div></label></li>';
				return result;
			}
			myAttentionPage.pageOperate(json.data, { resultFn : getResult, dataNull : "<li>没有查找到我的关注数据!</li>", pageFootStyle : myAttentionPage.pageStyle.defaultStyle });
		}
		else if(json.result === "error_weibo") {
			alert(json.weiboMsg);
		}
		else if(json.result === "error") {
			alert("请求操作, 系统出现异常!");
		}
		unloading();
	}
	});
}

//获取我的粉丝
var myFansPage = new Page("myFansPage", myFans, "bodyInfo", "bodyInfoPage");
myFansPage.size = 50;
myFansPage.pageSizeArr = [50, 70, 90];
//处理Page标签的函数
function myFans(_page) {
	if(_page != undefined) {
		myFansPage.page = _page;
	}
	jQuery.ajax({
		url : webroot + "/fans/json/myFans.shtml",
		data : {
		"page" : myFansPage.page,
		"size" : myFansPage.size
	},
	type : "POST",
	dataType : "json",
	async : true,
	beforeSend: function(){
		//提交处理的函数
		myFansPage.pageBeforeSend("<li>加载我的粉丝中...</li>");
		loading("加载我的粉丝中...");
	},
	error : function(json){
		myFansPage.pageError("<li>加载我的粉丝出错了~</li>");
		unloading();
	},
	//返回的是一个对象
	success : function(json){
		if(json.result === "success") {
			//处理数据列表的行信息
			function getResult(obj) {
				var result = '<li><label><div class="floatleft"><input type="checkbox" class="weiboChk" value="' + obj.fansattentionusername + '"/><img width="50" height="50" src="' + obj.img + '"/></div><div class="floatright"><p title="' + obj.nickname + '"><a href="http://weibo.com/u/' + obj.fansattentionusername + '" target="_banlk">' + obj.nickname + '</a></p>';
				result += '<p><img src="' + webroot + '/resources/images/' + (obj.gender === '男' ? 'user_man.png' : 'user_woman.png') + '" alt="' + obj.gender + '" title="' + obj.gender + '"/>&nbsp;';
				if(obj.verified === 'true') {
					result += '<img src="' + webroot + '/resources/images/weibo/weibo_v_org.png" alt="已V认证" title="已V认证"/>';
				}
				result += '</p></div></label></li>';
				return result;
			}
			myFansPage.pageOperate(json.data, { resultFn : getResult, dataNull : "<li>没有查找到我的粉丝数据!</li>", pageFootStyle : myFansPage.pageStyle.defaultStyle });
		}
		else if(json.result === "error_weibo") {
			alert(json.weiboMsg);
		}
		else if(json.result === "error") {
			alert("请求操作, 系统出现异常!");
		}
		unloading();
	}
	});
}